//
//  RHPlantSystemListCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/12/30.
//

#import "RHAssetSystemListCell.h"
@interface RHAssetSystemListCell()
@property (nonatomic ,strong) QMUILabel *systemNameLab;//系统名称

@property (nonatomic ,strong) QMUILabel *systemNumberLab;//编号

@property (nonatomic ,strong) QMUILabel *plantNameLab;//电站名称

@property (nonatomic ,strong) QMUILabel *deviceCountLab;//下级设备数量

@end
@implementation RHAssetSystemListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}


-(void)setModel:(RHMeSystemModel *)model
{
    _model = model;
    self.systemNameLab.text   = model.systemName;
    self.systemNumberLab.text = model.systemNumber;
    self.plantNameLab.text    = ValidNullDataToString(model.plantName, @"--");
    self.deviceCountLab.text  = ValidNullDataToString(model.deviceNumber, @"--");
}

- (void)setupUI{

    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    kWeakSelf;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];

    _systemNameLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    [bgView addSubview:_systemNameLab];
    [_systemNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(15);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(weakSelf.systemNameLab.mas_bottom).offset(15);
        make.height.offset(1);
    }];
    
    NSArray *titles = @[@"系统编号".rh_localized,
                        @"电站名称".rh_localized,
                        @"下级设备".rh_localized];
    __block UIView *lastView;
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [self itemViewWithTitle:title index:idx];
        [bgView addSubview:itemView];
        if (lastView) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_offset(0);
                make.top.equalTo(lastView.mas_bottom).offset(15);
            }];
        }else{
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_offset(0);
                make.top.equalTo(line.mas_bottom).offset(15);
            }];
        }
        lastView = itemView;
    }];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-15);
    }];
}

-(UIView*)itemViewWithTitle:(NSString*)title  index:(NSInteger)index
{
    UIView *bgView = kView;
    QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    titleLab.text = [NSString stringWithFormat:@"%@:",title];
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    
    
    QMUILabel *contentLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentRight];
    contentLab.text = @"--";
    contentLab.backgroundColor = kCOLOR_HEX(@"ffffff");
    contentLab.layer.cornerRadius = 8;
    contentLab.layer.masksToBounds = YES;
    [bgView addSubview:contentLab];

    [contentLab sizeToFit];
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab.mas_right).offset(-5);
        make.top.mas_offset(0);
        make.bottom.mas_offset(0);
        make.right.mas_offset(-15);
    }];
    
    if (index == 0) {
        self.systemNumberLab = contentLab;
    }
    if (index == 1) {
        self.plantNameLab = contentLab;
    }
    if (index == 2) {
        self.deviceCountLab = contentLab;
    }
    return bgView;
}

@end
